﻿using DalatCooking.Core;
using DalatCooking.Core.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Category_Add_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected override void OnInit(EventArgs e)
    {
        ListItem lx = new ListItem();
        lx.Value = "0";
        lx.Text =  "Chọn danh mục";
        lx.Attributes.Add("data-image", "ThumbnailHandler.axd?img=/uploads/images/noimage.png");
        lx.Attributes.Add("data-description", "Chọn danh mục");
        ddlMenuParent.Items.Add(lx);
        LoadParentDropDown(ddlMenuParent, 0);
        if (CategoryID != -1)
        {
            BinDataInfo();
        }
        base.OnInit(e);
    }
    static int level = -1;
    public static void LoadParentDropDown(DropDownList ddl, int parentID)
    {
        level++;
        System.Text.StringBuilder appender = new System.Text.StringBuilder();
        for (int j = 0; j < level; j++)
        {
            appender.Append("&nbsp;&nbsp;&nbsp;&nbsp;");
        }
        if (level > 0)
        {
            appender.Append("|__");
        }
        List<Category> list = CategoryManager.FindAll().FindAll(ca => ca.ParentId == parentID && ca.IsActive == true);
        
        for (int i = 0; i < list.Count; i++)
        {
            ListItem l = new ListItem();
            l.Value = list[i].CategoryId.ToString();
            l.Text = HttpUtility.HtmlDecode(appender.ToString()) + list[i].Name;
            l.Attributes.Add("data-image", "ThumbnailHandler.axd?img=" + list[i].Picture + "");
            l.Attributes.Add("data-description", HttpUtility.HtmlDecode(appender.ToString()) + list[i].Name);

            // ddl.Items.Add(new ListItem(HttpUtility.HtmlDecode(appender.ToString()) + list[i].Name, list[i].CategoryId.ToString()));

            ddl.Items.Add(l);
            LoadParentDropDown(ddl, list[i].CategoryId); // Goi de quy ham              
        }
        level--;
    }
    protected string GetParentTitle(object obj)
    {
        Category temp = CategoryManager.Single(int.Parse(obj.ToString()));
        if (temp == null)
            return "";
        else
            return StringHelper.RemoveDiacriticsURL(temp.Name);
    }
    public string GetCategoryUrl(object CategoryID)
    {
        string html = "";
        html += "" + StringHelper.RemoveDiacriticsURL(CategoryManager.Single(int.Parse(CategoryID.ToString())).Name) + "-" + CategoryID.ToString();
        return html;
    }
    public string GetContentUrl(object ContentID)
    {
        string html = "";
        // html += "" + StringHelper.RemoveDiacriticsURL(Categories.Single(Contents.Single(ContentID).CategoryID).CategoryName) + "/" + Contents.Single(ContentID).Slug + "/" + ContentID.ToString();
        return html;
    }

    void BinMenuParent()
    {
        foreach (var item in MenuManager.FindAll().FindAll(c => c.ParentID == 0))
        {
            ddlMenuParent.Items.Add(new ListItem(item.Name, item.MenuID.ToString()));
            foreach (var item1 in MenuManager.FindAll().FindAll(c => c.ParentID == 0))
            {
                ddlMenuParent.Items.Add(new ListItem("----" + item1.Name, item1.MenuID.ToString()));
            }
        }
    }
    void BinDataInfo()
    {
        Category m = CategoryManager.Single(CategoryID);
        if (m != null)
        {
            txtName.Text = m.Name;
            txtSortIndex.Text = m.SortIndex.ToString();
            imgthumb.Src = m.Picture;
            txtPicture.Text = m.Picture;
            txtDescription.Text = m.Description;

            ddlMenuParent.SelectedValue = m.ParentId.ToString();
            cbIsActive.Checked = m.IsActive;
            txtSortIndex.Text = m.SortIndex.ToString();
        }
    }
    public int CategoryID
    {
        get
        {
            if (Request.QueryString["CategoryID"] != null)
                return ConvertObject.ToInt(Server.HtmlEncode(Request.QueryString["CategoryID"]));
            else return -1;
        }
    }
    void Save()
    {
        Category m = new Category();
        m.CategoryId = -1;
        m.Name = txtName.Text;
        m.Description = txtDescription.Text;
        m.Picture = txtPicture.Text;
        m.IsActive = cbIsActive.Checked;
        m.ParentId = ConvertObject.ToInt(ddlMenuParent.SelectedValue);

        if (CategoryManager.Insert(m))
            Response.Redirect("Default.aspx");
    }
    void Update()
    {
        Category m = CategoryManager.FindById(CategoryID);

        m.Name = txtName.Text;
        m.Description = txtDescription.Text;
        m.Picture = txtPicture.Text;
        m.IsActive = cbIsActive.Checked;
        m.SortIndex = ConvertObject.ToInt(txtSortIndex.Text);
        m.ParentId = ConvertObject.ToInt(ddlMenuParent.SelectedValue);
        if (CategoryManager.Update(m))
            Response.Redirect("Default.aspx", true);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (CategoryID != -1)
        {
            Update();
        }
        else
        {
            Save();
        }
    }
    protected void btnCategory_Click(object sender, EventArgs e)
    {
        /*
        Menus m = new Menus();
        m.UniqueName = StringHelper.RemoveWhitespace(txtCategory.Text.Trim());
        m.Name = txtCategory.Text;
        if (Menus.MenuType_Add(m) > 0)
            Response.Redirect(Request.RawUrl);
         * */
    }
}